package com.hyl.domain.auth.service;

import com.google.common.cache.Cache;
import com.hyl.domain.auth.adapter.port.ILoginPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author hyl
 * @description
 * @create 2025-06-11 14:27
 */
@Slf4j
@Service
public class WeixinLoginService implements ILoginService{

    @Resource
    private ILoginPort loginPort;
    @Resource
    private Cache<String, String> openidToken;
    @Override
    public String creatQrCodeTicket() throws Exception {

        return loginPort.creatQrCodeTicket();

    }

    @Override
    public String checkLogin(String ticket) {
        return openidToken.getIfPresent(ticket);
    }

    @Override
    public void saveLoginState(String ticket, String openid) throws IOException {
        // 实际的业务场景，openid 可以生成 jwt 的 token 让前端存储
        openidToken.put(ticket, openid);
        // 发送登录成功模板消息
        loginPort.sendLoginTempleteMessage(openid);
    }
}
